DEFINITION MODULE Files;

(* (C) Copyright 1987,1988 Fitted Software Tools. All rights reserved. *)

(*
    DOS File Management Interface module

    If more information about these procedures, error codes, etc is
    needed, please consult the DOS technical reference manual.
*)

FROM SYSTEM IMPORT ADDRESS;

CONST (* the standard files *)

    StdIn  = 0;
    StdOut = 1;
    StdErr = 2;
    StdCom = 3;
    StdPrn = 4;


CONST (* file attribute bits - to be used on Create call.
         You may add several of these attributes together
       *)

    NORMAL      = {};
    READONLY    = {0};
    HIDDEN      = {1};
    SYSTEM      = {2};


CONST (* file open mode flags - for building mode flags for the Open call.
         You must pick 1 from the first group.
         You may pick one more from each of the other groups and add
         them all together
       *)

    READ    = {};               (* open file for reading *)
    WRITE   = {0};              (* open file for writing *)
    IO      = {1};              (* open file for reading and writing *)

        (* the following are for use under DOS 3.x only *)

    NOINHERIT   = {7};          (* do not let child inherit this file *)

    DENYALL     = {4};          (* deny all access to other processes *)
    DENYWRITE   = {5};          (* deny write access to other processes *)
    DENYREAD    = {4,5};        (* deny read access to other processes *)
    DENYNONE    = {6};          (* allow free access to other processes *)


TYPE (* seek mode - for use with Seek *)

    SeekMode = ( SEEKABS,       (* seek to the location specified *)
                 SEEKCUR,       (* the location specified is relative to the
                                   value of the current file pointer *)
                 SEEKEND        (* the location specified is relative to the
                                   end of file *)
               );


VAR (* DOS error code for the last Files function *)

    FileStatus :CARDINAL;       (* =0 if the last call was successful *)


PROCEDURE Open( VAR fd :INTEGER; name :ARRAY OF CHAR; mode :BITSET );
(*
    Open a file according to mode.

    On return, fd will contain the file descriptor value returned by DOS,
    or -1 if an error occured.
*)

PROCEDURE Create( VAR fd :INTEGER; name :ARRAY OF CHAR; attr :BITSET );
(*
    Create a new file or truncate an existing one.

    If Create is successful, the file will be open for read and write and
    fd will contain the file descriptor value returned by DOS.
    If Create fails, fd will contain -1.
*)

PROCEDURE CreateTemp( VAR fd :INTEGER; VAR path :ARRAY OF CHAR; attr :BITSET );
(*
    Used under DOS 3.x to create a new uniquely named temporary file.
    On entry, path should contain the path name of the directory
    where the temporary file is to be placed and enough space (14
    bytes) where DOS will place the temporary name.

    On return, path will contain the complete name of the temporary
    file.

    NOTE: DOS does not automatically delete temporary files. You must
    delete the file once you no longer need it.
*)

PROCEDURE CreateNew( VAR fd :INTEGER; name :ARRAY OF CHAR; attr :BITSET );
(*
    Similar to Create, but fails if the file already exists (DOS 3.x).
*)

PROCEDURE Close( fd :INTEGER );
(*
    Close the file.
    Any further I/O using this file descriptor would be an error.
*)

PROCEDURE Read( fd :INTEGER; buffer :ADDRESS; bytes :CARDINAL;
                VAR nread :CARDINAL );
(*
    Read the specified number of bytes from the file.

    On return, nread contains the actual number of bytes read.
*)

PROCEDURE Write( fd :INTEGER; buffer :ADDRESS; bytes :CARDINAL;
                 VAR nwritten :CARDINAL );
(*
    Write the specified number of bytes to the file.

    On return, nwritten contains the actual number of bytes written.
*)

PROCEDURE Seek( fd :INTEGER; mode :SeekMode; VAR offset :LONGCARD );
(*
    Move the file pointer to the location specified by offset,
    according to the strategy specified in mode.

    On return, offset contains the new file pointer location.
*)

PROCEDURE Lock( fd :INTEGER; offset :LONGCARD; length :LONGCARD );
(*
    Lock the file region starting at offset for length bytes (DOS 3.x).
*)

PROCEDURE Unlock( fd :INTEGER; offset :LONGCARD; length :LONGCARD );
(*
    Unlock the file region starting at offset for length bytes (DOS 3.x).
*)

PROCEDURE Dup( fd :INTEGER; VAR newfd :INTEGER );
(*
    Return newfd, a new file descriptor for the file referenced by fd.
*)

PROCEDURE Dup2( fd, fd2 :INTEGER );
(*
    Force fd2 to refer to the same file as fd.
*)

PROCEDURE GetFileTime( fd :INTEGER; VAR time :LONGCARD );
(*
    returns in time the date and time of last modification of the file.

    time DIV 65536L = date of last modification
    time MOD 65536L = time of last modification
*)

PROCEDURE SetFileTime( fd :INTEGER; time :LONGCARD );
(*
    Sets the file's modification date and time.
*)

PROCEDURE Rename( oldname, newname :ARRAY OF CHAR );
(*
    Change the name of the file oldname to newname.

    Rename can be used to move a file to another subdirectory
    in the same drive.
*)

PROCEDURE Delete( name :ARRAY OF CHAR );
(*
    Delete the named file.
*)

END Files.